package login;

import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import utils.JsonUtil;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author sunzhihao2
 * @date 17:36 2018/5/23
 */
public class SimulationLogin {

    private static Logger logger = LoggerFactory.getLogger(SimulationLogin.class);

    //创建默认客户端
    private static CloseableHttpClient closeableHttpClient = HttpClients.createDefault();
    public static CloseableHttpClient login(){
        Map<String,Object> map = new HashMap<String,Object>();
        //创建Post请求实例
        HttpPost httpPost = new HttpPost("https://api.xuangubao.cn/api/account/mobile_login");
        //创建参数列表
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("Accept", "application/json, text/plain, */*"));
        nvps.add(new BasicNameValuePair("Accept-Encoding", "gzip, deflate, br"));
        nvps.add(new BasicNameValuePair("Accept-Language", "zh-CN,zh;q=0.9"));
        nvps.add(new BasicNameValuePair("UserAgent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36"));
        nvps.add(new BasicNameValuePair("Content-Type", "application/json;charset=UTF-8"));
        nvps.add(new BasicNameValuePair("Connection", "keep-alive"));
        nvps.add(new BasicNameValuePair("Mobile", "13998883228"));
        nvps.add(new BasicNameValuePair("Password", "szh279393"));

        //向对方服务器发送Post请求
        try {
            //将参数进行封装，提交到服务器端
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF8"));
            CloseableHttpResponse httpResponse = closeableHttpClient.execute(httpPost);

            //如果模拟登录成功
            if(httpResponse.getStatusLine().getStatusCode() == 200) {
                Header[] headers = httpResponse.getAllHeaders();
                for (Header header : headers) {
                    System.out.println(header.getName() + ": " + header.getValue());
                }

                return closeableHttpClient;
            }
            System.out.println("-------------------------------");

            httpPost.abort();      //释放资源

            return null;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            httpPost.abort();      //释放资源
        }
        return null;
    }

}
